<?php
	/**
	 * Обработка запроса на добавление необходимой покупки.
	 * 
	 * @author Костин Алексей Васильевич aka Volt(220)
	 * @copyright Copyright (c) 2010, Костин Алексей Васильевич
	 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU Public License
	 * @version 1.0
	 * @package HomeMoney
	 */

	/**
	 * Подключение настроек. 
	 */
	require_once("../ini.php");
	$DBCon=SQLDBFactory::getDB();


	$date=$DBCon->escapeString($_GET["date"]);
	$item=$DBCon->escapeString($_GET["item"]);
	$cat=$DBCon->escapeString($_GET["cat"]);
	$srok=$DBCon->escapeString($_GET["srok"]);
	$col=$_GET["col"]+0;
	$prim=$DBCon->escapeString($_GET["prim"]);

	$res=$DBCon->select("select id from srok where srok=$srok");
	if ($mas=mysql_fetch_row($res)){
		$idSrok=$mas[0];
	}
	else{
		$idSrok=$DBCon->insert("insert into srok(srok) values ($srok)");	
	}

	$res=$DBCon->select("select id from categ where cat=$cat");
	if ($mas=mysql_fetch_row($res)){
		$idCat=$mas[0];
	}
	else{
		$idCat=$DBCon->insert("insert into categ(cat) values ($cat) ");	
	}
	
	$res=$DBCon->select("select id from goods where item=$item and id_cat=$idCat");
	if ($mas=mysql_fetch_row($res)){
		$idGood=$mas[0];
	}
	else{
		$idGood=$DBCon->insert("insert into goods(item, id_cat) values ($item, $idCat)");	
	}
		
	$idNeed=$DBCon->insert("insert into needToBuy(id_good,id_srok,col, date, prim) values ($idGood,$idSrok, $col, $date,$prim) ");

	$mas["mes"]="$item Сохранено";
	$mas["id"]=$idNeed;
	echo json_encode($mas);
/*SELECT item, cat, name, address, col, price, basket.sum, buys.id, buys.sum
FROM basket
LEFT OUTER JOIN goods ON goods.id = basket.id_good
LEFT OUTER JOIN buys ON buys.id = basket.id_buy
LEFT OUTER JOIN shops ON shops.id = buys.id_shop
LEFT OUTER JOIN categ ON goods.id_cat = categ.id
LIMIT 0 , 900*/
?>
